#!/bin/bash

# 修复 Gitee SSH 连接问题

echo "🔧 修复 Gitee SSH 连接..."
echo ""

# 检查 SSH 密钥
if [ -f ~/.ssh/id_ed25519.pub ]; then
    KEY_FILE=~/.ssh/id_ed25519.pub
elif [ -f ~/.ssh/id_rsa.pub ]; then
    KEY_FILE=~/.ssh/id_rsa.pub
else
    echo "❌ 未找到 SSH 公钥"
    echo "生成新的 SSH 密钥..."
    read -p "请输入你的邮箱: " EMAIL
    ssh-keygen -t ed25519 -C "$EMAIL" -f ~/.ssh/id_ed25519
    KEY_FILE=~/.ssh/id_ed25519.pub
fi

echo "✅ 找到 SSH 公钥: $KEY_FILE"
echo ""
echo "📋 请将以下公钥添加到 Gitee:"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
cat "$KEY_FILE"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""
echo "📝 添加步骤:"
echo "1. 访问: https://gitee.com/profile/ssh_keys"
echo "2. 点击 '添加公钥'"
echo "3. 复制上面的公钥内容"
echo "4. 粘贴到 '公钥' 输入框"
echo "5. 填写标题（如：MacBook）"
echo "6. 点击 '确定'"
echo ""
read -p "添加完成后，按回车继续测试连接..."

# 测试连接
echo ""
echo "🔍 测试 SSH 连接..."
if ssh -T git@gitee.com 2>&1 | grep -q "successfully authenticated"; then
    echo "✅ SSH 连接成功！"
    echo ""
    echo "现在可以推送代码了:"
    echo "  git push -u origin main"
else
    echo "❌ SSH 连接仍然失败"
    echo ""
    echo "可能的原因:"
    echo "1. 公钥未正确添加到 Gitee"
    echo "2. SSH 密钥权限问题"
    echo ""
    echo "可以尝试:"
    echo "1. 检查 ~/.ssh 目录权限: chmod 700 ~/.ssh"
    echo "2. 检查密钥文件权限: chmod 600 ~/.ssh/id_ed25519"
    echo "3. 或者使用 HTTPS 方式（见下方）"
fi

